package cn.com.w.web.controller;

import cn.com.w.common.VO.BaseVO;
import cn.com.w.dao.Entity.TTag;
import cn.com.w.dao.Service.ITTagService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/tag")
@Api(tags = "博客标签维护")
public class TagController {
    private final ITTagService tTagService;

    public TagController(ITTagService tTagService) {
        this.tTagService = tTagService;
    }

    @PostMapping("/add")
    public BaseVO<Boolean> addTag(@RequestHeader(value = "Authorization") String token, @RequestParam String name) {
        return BaseVO.<Boolean>builder()
                .code(200)
                .content(tTagService.save(TTag.builder().name(name).build()))
                .build();
    }

    @PostMapping("/delete")
    public BaseVO<Boolean> deleteTag(@RequestHeader(value = "Authorization") String token, @RequestParam Long id) {
        return BaseVO.<Boolean>builder()
               .code(200)
               .content(tTagService.removeById(id))
               .build();
    }

    @PostMapping("/update")
    public BaseVO<Boolean> updateTag(@RequestHeader(value = "Authorization") String token, @RequestParam Long id, @RequestParam String name) {
        return BaseVO.<Boolean>builder()
              .code(200)
              .content(tTagService.updateById(TTag.builder().id(id).name(name).build()))
              .build();
    }

    @GetMapping("/list")
    public BaseVO<?> listTag(@RequestHeader(value = "Authorization") String token) {
        return BaseVO.builder()
             .code(200)
             .content(tTagService.list())
             .build();
    }
}
